package com.example.mymarket.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.mymarket.Data_model.NewGoodsList;
import com.example.mymarket.Fragment.HomeFragment;
import com.example.mymarket.R;

import java.util.List;

public class NewGoodsAdapter extends BaseAdapter {
    public List<NewGoodsList> newGoodsList;
    private Context ctx ;
    private LayoutInflater inflater; //findViewById的另一种写法

    public NewGoodsAdapter(Context ctx, List<NewGoodsList> newGoodsList) {
        this.ctx = ctx;
        this.newGoodsList = newGoodsList;
    }

    @Override
    public int getCount() {
        return this.newGoodsList.size();
    }

    @Override
    public Object getItem(int i) {
        return this.newGoodsList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;

        if (view == null){
            view = LayoutInflater.from(this.ctx).inflate(R.layout.home_newgoods_item,null);
            holder = new ViewHolder();

            holder.iv_newGoods_picUrl = view.findViewById(R.id.iv_newGoods_picUrl);
            holder.tv_newGoods_name = view.findViewById(R.id.tv_newGoods_name);
            holder.tv_newGoods_retailprice = view.findViewById(R.id.tv_newGoods_retailprice);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        Glide.with(view.getContext())
                .load(this.newGoodsList.get(i).getPicUrl())
                .into(holder.iv_newGoods_picUrl);
        holder.tv_newGoods_name.setText(this.newGoodsList.get(i).getName());
        holder.tv_newGoods_retailprice.setText(this.newGoodsList.get(i).getCounterPrice().toString());

        return view;
    }
    static class ViewHolder{
        public ImageView iv_newGoods_picUrl;
        public TextView tv_newGoods_name;
        public TextView tv_newGoods_retailprice;
    }
}
